package com.dingding.service.systemservice.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResGetBanner;
import com.dingding.bean.entity.Banner;
import com.dingding.common.core.config.SystemCode;
import com.dingding.service.BaseService;
import com.dingding.service.systemservice.IBannerService;

/**
 * 
 * @ClassName: BannerServiceImpl
 * @Description: Banner相关Service实现类
 * @author zhaochenguang
 * @date 2015-10-22 下午04:35:44
 * @version 叮叮搭车V2.0
 */
public class BannerServiceImpl extends BaseService implements IBannerService {

	// 日志
	private Logger log = Logger.getLogger(BannerServiceImpl.class);

	/**
	 * @Title: getBanner
	 * @Description: 获取Banner
	 * @param dataResponse
	 *            响应信息封装
	 * @author zhaochenguang
	 * @return void
	 * @throws
	 * @Override
	 */
	@Transactional(readOnly = true)
	public void getBanner(DataResponse dataResponse) {

		List<Banner> bannerList = null;

		try {
			// 查询所有Banner
			bannerList = super.bannerDao.queryAll(false);
		} catch (Exception e) {
			log.error("获取Banner产生异常！BannerServiceImpl：getBanner()", e);
			//设置错误信息
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 生成响应Bean
		List<ResGetBanner> resGetBanners = new ArrayList<ResGetBanner>();
		// 封装响应信息
		boolean isSuccess=super.bannerDao.setResGetBannerList(bannerList, resGetBanners);
		if(isSuccess){
		// 设置响应body
		dataResponse.setResponseBody(resGetBanners);
		// 设置响应信息
		setStatus(dataResponse, SystemCode.SUCCESS);
		}
		else{
			setStatus(dataResponse, SystemCode.BANNER_NULL);
		}

	}


	@Override
	public void setStatus(DataResponse dataResponse, int status) {
		switch (status) {
		// 成功
		case SystemCode.SUCCESS:
			dataResponse.setResponseCode(SystemCode.SUCCESS);
			dataResponse.setResponseMessage(SystemCode.SUCCESS_MESSAGE);
			break;
		// 错误
		case SystemCode.ERROR:
			dataResponse.setResponseCode(SystemCode.ERROR);
			dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
			break;
		// 获取到的Banner列表为空
		case SystemCode.BANNER_NULL:
			dataResponse.setResponseCode(SystemCode.BANNER_NULL);
			dataResponse.setResponseMessage(SystemCode.BANNER_NULL_MESSAGE);
			break;
		// 默认
		default:
			dataResponse.setResponseCode(SystemCode.ERROR);
			dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
			break;
		}

	}

}
